/**
 * @Author：ming
 * @Date：2023/9/26 2:06 PM
 * @Filename：Lc49
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author ming
 */
public class Lc49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> lists = new ArrayList<>();

        HashMap<String, Integer> map = new HashMap<>();

        int count = 0;
        for (int i = 0; i < strs.length; i++) {
            boolean add = false;
            String s = getOrder(strs[i]);
            for (String s1 : map.keySet()) {
                if(s1.equals(s)){
                    lists.get(map.get(s1)).add(strs[i]);
                    add = true;
                }
            }

            if(!add){
                map.put(s,count++);
                ArrayList<String> l = new ArrayList<>();
                l.add(strs[i]);
                lists.add(l);
            }

        }

        return lists;

    }


    public static String getOrder(String s){
        char[] c = s.toCharArray();
        Arrays.sort(c);
        return new String(c);
    }
}
